﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using ZXing;
using ZXing.QrCode;
using UnityEngine.UI;

public class EditorUIController : MonoBehaviour {

    public RawImage image;

     Texture2D encoded;
     string Lastresult;

    public void OnBackButtonClicked()
    {
        SceneManager.LoadScene("SampleScene");
    }
    
    // Use this for initialization
	void Start () {
        encoded = new Texture2D(256, 256);
        Invoke("MyUpdateQRCode", 1);
    }


    private static Color32[] Encode(string textForEncoding, int width, int height)
    {
        var writer = new BarcodeWriter
        {
            Format = BarcodeFormat.QR_CODE,
            Options = new QrCodeEncodingOptions
            {
                Height = height,
                Width = width
            }
        };
        return writer.Write(textForEncoding);
    }
    void MyUpdateQRCode()
    {
        Lastresult = StaticVariable.currentShopId;

        var textForEncoding = Lastresult;
        if (textForEncoding != null)
        {
            var color32 = Encode(textForEncoding, encoded.width, encoded.height);
            image.texture = encoded;

            encoded.SetPixels32(color32);
            encoded.Apply();

        }
    }


    // Update is called once per frame
    void Update () {
		
	}
}
